/*package com.glgc.projecti.web.controller;

import java.net.URLEncoder;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glgc.projecti.common.CommonConstant;
import com.glgc.projecti.common.CommonUtils;
import com.glgc.projecti.common.HttpRequestTool;
import com.glgc.projecti.common.WeixinUtils;
import com.glgc.projecti.web.controller.base.BaseController;

@Controller
@RequestMapping(value="/weixin")
public class WeixinController extends BaseController {
	
	private final static Logger logger = LoggerFactory.getLogger(WeixinController.class);
//	https://open.weixin.qq.com/connect/oauth2/authorize?
//	appid=wxfd242c6a724a7021&redirect_uri=http%3A%2F%2Fhelp.glsc.com.cn%2Fstockrace%2Fweixin_oauth
//	&response_type=code&scope=snsapi_base&state=&connect_redirect=1#wechat_redirect 
	
	@RequestMapping(value = "/common_menu.do", method = RequestMethod.GET, produces = "text/html")
	public String commonMenu(@RequestParam(value="state")String state) throws Exception {
		String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?"
		+ "appid="+ResourceBundle.getBundle("config").getString("app_wx_id")+"&redirect_uri="+URLEncoder.encode(ResourceBundle.getBundle("config").getString("app_wx_auth"), "UTF-8")
		+ "&response_type=code&scope=snsapi_base&state=" + state + "&connect_redirect=1#wechat_redirect";
		logger.debug("http request: " + authUrl);
		
		return "redirect:" + authUrl;
	}

    *//**
     * 取得code以后的默认跳转页面，判断是否绑定过，若绑定则直接登录
     * @throws Exception 
     *//*
    @RequestMapping(value = "/weixin_oauth.do", method = RequestMethod.GET)
    public String judgeLogin(@RequestParam(value = "code", required = true) String code,
                                   @RequestParam(value = "state", required = true) String state,
                                   HttpServletRequest request) throws Exception {

        logger.info("获取Auth的code是:[{" + code + "}]");
        logger.info("获取Auth的state是:[{" + state + "}]");

        //通过code换取网页授权access_token
        // 获取到网页授权access_token的同时，也获取到了openid，snsapi_base式的网页授权流程即到此为止。
        String apiUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        apiUrl = String.format(apiUrl, ResourceBundle.getBundle("config").getString("app_wx_id"), ResourceBundle.getBundle("config").getString("app_wx_secret"), code);
        logger.info("获取Auth的ApiUrl是:" + apiUrl);

        String json = HttpRequestTool.httpRequest(apiUrl, RequestMethod.POST.name(), null);
        logger.info("JSON: " + json);
        
        //获取openId
        String openId ="";
        JSONObject resMap = new JSONObject();
 		ObjectMapper om = new ObjectMapper();
 		if (json != null) {
 			try {
 				resMap = om.readValue(json.getBytes("UTF-8"), JSONObject.class);
 			} catch (Exception e) {
 				e.printStackTrace();
 			} 
 		}
 		
 		 openId = resMap.getString("openid");
 	 
 		logger.info("JSON: " + resMap);
        
        
        String returnUrl = "";
        String error_code = "1";
        
        if(!CommonUtils.isEmpty(openId)){
        	request.getSession().setAttribute("openId",openId);
        }
        if (CommonUtils.isEmpty(openId)) {
        	//首页
        	returnUrl = "../race/login.do"; 
        	error_code = "0";
        	
        } else if (CommonConstant.WEIXIN_STATE_REGISTER.equals(state)) {
        	//报名页面
        	returnUrl = "../race/register.do";
        } else if (CommonConstant.WEIXIN_STATE_LOGIN.equals(state)) {
        	//登陆页面
        	returnUrl = "../race/login.do";
        } else if (CommonConstant.WEIXIN_STATE_PERSONAL_PAGE.equals(state)) {
        	//个人页面
        	returnUrl = "../race/login.do";
        } else if (CommonConstant.WEIXIN_STATE_GUAGUA_CARD.equals(state)) {
        	//刮刮卡
        	returnUrl = "../race/6-ggk.do";
        } else if (CommonConstant.WEIXIN_STATE_SHARE_PAGE.equals(state)) {
        	//分享
        	returnUrl = "../race/login.do";
        }else if(CommonConstant.WEIXIN_STATE_HOME_PAGE.equals(state)){
        	returnUrl = "../race/racehome.do";
        }else if(CommonConstant.WEIXIN_STATE_RANK_PAGE.equals(state)){
        	returnUrl = "../race/rank.do";
        }else if(CommonConstant.WEIXIN_STATE_WHEEL_PAGE.equals(state)){
        	returnUrl = "../race/wheel.do";//抽奖页面
        }else if(CommonConstant.WEIXIN_STATE_WXSHARE_PAGE.equals(state)){
        	returnUrl = "../race/wx_share.do";//分享页面
        }else if(CommonConstant.WEIXIN_STATE_QUESTIONINDEX_PAGE.equals(state)){
        	returnUrl = "../nmq/index.do";  	//  跳转问卷页面      	
        }else if(CommonConstant.WEIXIN_STATE_QUESTIONWHELL_PAGE.equals(state)){
        	returnUrl = "../nmq/wheel.do";	//  跳转问卷抽奖页面
        }else if(CommonConstant.WEIXIN_STATE_QUESTIONSHARE_PAGE.equals(state)){
        	returnUrl = "../nmq/share.do";	//  跳转问卷抽奖页面
        }
        
        logger.info(error_code+"::"+returnUrl);
        
		return "redirect:" + returnUrl;

    }
    
    @RequestMapping(value = "/wish.do", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public String shareWish(HttpServletRequest request, Model model) throws Exception {
    	Map<String, String> wish = WeixinUtils.wish(request);
    	ObjectMapper om = new ObjectMapper();
    	String json = om.writeValueAsString(wish);
    	logger.debug("wish json: " + json);
    	return json;
    }
	
    public static void main(String [] args) throws Exception {
    	 String json = "{\"access_token\":\"OezXcEiiBSKSxW0eoylIeOhCrfbfTZs_CuKf2sPCV4KUUZp6U_qO3mMrn_NA20wfYsdB6o52pRrgK1PTB8A7rqaTbxQFC7Q8R83V121LPt_e9tMdxTKI0tGZElc460H-1Gs82nUNdI0bqpu4HKRRsg\",\"expires_in\":7200,\"refresh_token\":\"OezXcEiiBSKSxW0eoylIeOhCrfbfTZs_CuKf2sPCV4KUUZp6U_qO3mMrn_NA20wfGeE2_V_xxIdQn8DRA99odHk5iohS4Xzx2sxU_U9bo1W56o-NFZlSbuQAi5K9cN3VOVBxrSmlkEDCXgNZN3uOUA\",\"openid\":\"odb6ujsGXcamlrQ4pSChd5sirz5A\",\"scope\":\"snsapi_base\"}";
    	 
    	 JSONObject resMap = new JSONObject();
 		ObjectMapper om = new ObjectMapper();
 		if (json != null) {
 			try {
 				resMap = om.readValue(json.getBytes("UTF-8"), JSONObject.class);
 			} catch (Exception e) {
 				e.printStackTrace();
 			} 
 		}
 		 
    	 
    	 
    	 System.out.println("JSON: " + resMap.getString("openid"));
    	 
    	 
    	  ObjectMapper objectMapper = new ObjectMapper();
          
          JsonNode jsonNode = objectMapper.readTree(json);
          
          //获取openId
          String openId = jsonNode.get("openid") == null ? "" : jsonNode.get("openid").toString();
          String errcode = jsonNode.get("errcode") == null ? "" : jsonNode.get("errcode").toString();
          String errmsg = jsonNode.get("errmsg") == null ? "" : jsonNode.get("errmsg").toString();
          System.out.println("返回的openid是:" + openId);
          System.out.println("返回的errcode是:" + errcode);
          System.out.println("返回的errmsg是:" + errmsg);
    	  
    }
}

*/